﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Web;
using MFTool;
using MF_Base;
using UIBase;

namespace AutoUI.Areas.ConfigUI.EasyUICtrl
{
    public class SubDataGrid : CtrlBase
    {
        private const string _toolBarHtm = @"<div id='toolBar{0}'>
            <table cellpadding='0' cellspacing='0' style='width: 100%'>
                <tr>
                    <td>
                        <a href='#' class='easyui-linkbutton' onclick=""javascript: $('#{0}').datagrid('mfInsertRow')"" iconcls='iconfont iconfont-jia1' plain='true'>增加</a>
                        <a href='#' class='easyui-linkbutton' onclick=""javascript: $('#{0}').datagrid('mfDeleteRow')"" iconcls='iconfont iconfont-shanchu' plain='true'>删除</a>
                        <a href='#' class='easyui-linkbutton' onclick=""javascript: $('#{0}').datagrid('mfMoveUp')"" iconcls='iconfont iconfont-xiangshang' plain='true'>上移</a>
                        <a href='#' class='easyui-linkbutton' onclick=""javascript: $('#{0}').datagrid('mfMoveDown')"" iconcls='iconfont iconfont-paixu' plain='true'>下移</a>
                    </td>
                </tr>
            </table>
        </div>";

        public SubDataGrid(string id, EasyUICtrlPrepareData pData)
            : base(id, pData)
        {
            //_colList = collist;
        }
        public override void Prepare()
        {
            DomStr = "table";
            //Attr.Add()
            _class.Add("easyui-datagrid");
            if (_dataOption.GetValue("showToolBar") == "true")
            {
                _attr.SetValue("toolbar", "#toolBar" + Id);
            }

            _dataOption.GetValue("showToolBar");//是否显示操作栏
            _attr.SetValue("width", "100%");

            InnerHtm = "<thead><tr>";
            if (!string.IsNullOrEmpty(_dataOption.GetValue("checkbox")) && _dataOption.GetValue("checkbox") == "true")
            {
                InnerHtm += "<th data-options=\"field:'ck',checkbox:true\"></th>";
                _attr.Remove("checkbox");
            }

            string columnSetting = _dataOption.GetValue("ColumnSetting");
            if (!string.IsNullOrEmpty(columnSetting))
            {
                List<Dictionary<string, object>> collist = columnSetting.JsonToDictionaryList();
                foreach (var colDic in collist)
                {
                    if (colDic.GetValue("IsVisible") != "是")
                        continue;
                    colDic.Remove("IsVisible");

                    var th = "<th ";

                    colDic.GetValue("FieldType");
                    colDic.Remove("FieldType");

                    if (colDic.GetValue("Enable") == "是")
                    {
                        string controlType = colDic.GetValue("CtrlType");
                        if (!string.IsNullOrEmpty(controlType) && controlType != GridControlType.HiddenInput.ToString())
                        {                            
                            colDic.GetValue("Detail");
                            th += string.Format("editor=\"{0}\" ", GetEditorHtm(controlType, colDic.GetValue("FieldName"), colDic.GetValue("Detail"), colDic.GetValue("ColumnDetail")));
                        }
                    }
                    colDic.Remove("Enable");
                    colDic.Remove("Detail");
                    colDic.Remove("CtrlType");

                    colDic.GetValue("FieldName");
                    th += string.Format("field='{0}' ", colDic.GetValue("FieldName"));
                    colDic.Remove("FieldName");

                    foreach (var p in colDic)
                    {
                        th += string.Format("{0}='{1}' ", p.Key, p.Value);
                    }
                    th += " halign='center' ";
                    th += string.Format(">{0}</th>", colDic.GetValue("ColumnName"));
                    colDic.Remove("ColumnName");
                    InnerHtm += th;
                }
                InnerHtm += "</tr></thead>";
            }            
        }

        protected override void AfterGeneralHtml(ref string html)
        {
            if (_dataOption.GetValue("showToolBar") == "true")
                html = _toolBarHtm.ReplaceArg(Id) + html;
        }

        public override string GetScript()
        {
            string script = "";
            string columnSetting = _dataOption.GetValue("ColumnSetting");
            List<Dictionary<string, object>> collist = columnSetting.JsonToDictionaryList();
            foreach (var colDic in collist)
            {
                string detail = colDic.GetValue("Detail");
                var dic = detail.JsonToDictionary();
                EasyUICtrlPrepareData pData = new EasyUICtrlPrepareData()
                {
                    DataOptions = dic
                };
                var colCtrl = EasyUICtrlFactory.GetCtrl(colDic.GetValue("CtrlType"), "", pData);
                string tmpScript = colCtrl.GetGridScript(colDic.GetValue("FieldName"), this.Id);
                if(!string.IsNullOrEmpty(tmpScript))
                {
                    script +=  tmpScript + "\r\n";
                }                
            }
            script += "$('#{0}').datagrid().datagrid('enableCellEditing');\r\n".ReplaceArg(Id);
            return script;
        }

        private string GetEditorHtm(string controlType, string fieldName, string detail, string columnDetail)
        {
            var detailDic = detail.JsonToDictionary();
            var columnDetaildic = columnDetail.JsonToDictionary();
            foreach(var p in columnDetaildic)
            {
                detailDic.SetValue(p.Key, p.Value);
            }
            EasyUICtrlPrepareData pData = new EasyUICtrlPrepareData()
            {
                DataOptions = detailDic
            };
            var colCtrl = EasyUICtrlFactory.GetCtrl(controlType, "", pData);
            var subGridCtrlType = EasyUICtrlFactory.GetSubGridCtrlType(controlType);
            string opt = colCtrl.GetGridOptionJson(fieldName);
            return "{{ type: '{0}',options:{{{1}}} }}".ReplaceArg(subGridCtrlType, opt);
        }

        protected override void BeforeDataOptionToJson(ref Dictionary<string, object> dataOption)
        {
             dataOption.Remove("ColumnSetting");
        }

        protected override void AfterDataOptionToJson(ref string dataOption)
        {
            
        }
    }
}